﻿/// <reference path="q.js" />
/// <reference path="jquery-2.0.3.js" />
/// <reference path="class.js" />
var HoodNews = HoodNews || {};

HoodNews.controler = (function () {
    var Controler = Class.create({
        init: function (dataPersister, userInterface) {
            this.dataPersister = dataPersister;
            this.ui = userInterface;
            this.articles = [];
            this.activeArticle = {};
            this.myArticles = [];
            this.myActiveArticle = {};
        },
        start: function () {
            this.attachHandlers();
            var self = this;

            this.loadArticles();
            //this.
            //if (this.dataPersister.user.isLoggedin()) {
            //    this.ui.loadGame(this.dataPersister.user._getNickname());
            //    this.updateGames();
            //} else {
            //    this.ui.loadLogin();
            //}
        },
        attachHandlers: function () {
            var $container = $(this.ui.selector);
            var self = this;

            var pubnub = PUBNUB.init({
                publish_key: 'pub-c-d9b56413-5a7f-4528-9e32-6324f449b10e',
                subscribe_key: 'sub-c-361a1a54-04c1-11e3-a005-02ee2ddab7fe',
                origin: 'pubsub.pubnub.com',
                //uuid: 'myCustomUUID'
            });

            pubnub.subscribe({
                channel: "my_channel",
                message: function (m) {
                    self.updateArticles();
                    self.updateMyArticles();
                    var $container = $("#notificationTab");
                    $container.css('background-color', 'red');
                    $container.text("New article!");
                    setTimeout(function () {
                        $container.css('background-color', 'white');
                        $container.text("notificationTab");
                    }, 1500);
                }
            });

            $container.on("click", "#login-button", function () {
                var user = $container.find("#login-username-tb").val();
                var password = $container.find("#login-password-tb").val();

                self.dataPersister.user.login(user, password)
                .then(function (success) {
                    self.ui.loadArticles(self.dataPersister.user._getNickname());
                    self.updateGames();
                }, function (error) {
                    self.showErrorMessage(error);
                }).done();
            });

            $container.on("click", "#articlesLink ", function (ev) {
                self.loadArticles();
            });

            $container.on("click", "#myArticlesLink ", function (ev) {
                self.loadMyArticles();
            });

            $container.on("click", "#articles a", function (ev) {
                var articleId = $(ev.target).data("id");

                self.dataPersister.article.get(articleId)
                .then(function (result) {
                    self.activeArticle = result;
                    self.ui.loadActiveArticleTab(self.activeArticle);
                }, function (error) {
                    self.showErrorMessage(error);
                }).done();
            });

            $container.on("click", "#myArticles a", function (ev) {
                var articleId = $(ev.target).data("id");

                self.dataPersister.article.get(articleId)
                .then(function (result) {
                    self.myActiveArticle = result;
                    self.ui.loadMyActiveArticleTab(self.myActiveArticle);
                }, function (error) {
                    self.showErrorMessage(error);
                }).done();
            });

            $container.on("click", "#createArticleButton", function (ev) {
                self.myActiveArticle = {
                    title: "Title goes here...",
                    content: "Content goes here...",
                    images: [],
                    id:0
                };
                self.ui.loadMyActiveArticleTab(self.myActiveArticle);
            });

            $container.on("click", "#saveArticleButton", function (ev) {
                var currentArticle = self.myActiveArticle;
                var title = $("#articleTitle").val();
                var content = $("#articleContent").val();
                var images = [];

                if (currentArticle.id == 0) {
                    self.dataPersister.article.create(title, content, images)
                    .done();
                } else {
                    self.dataPersister.article.update(currentArticle.id, title, content, images)
                    .done();
                }
            });
        },
        loadArticles: function () {
            var self = this;

            this.dataPersister.article.getAll(10, false).then(function (result) {
                self.articles = result;
            }).then(function () {
                self.dataPersister.article.get(self.articles[0].id).then(function (result) {
                    self.activeArticle = result;
                }).then(function () {
                    self.ui.loadMainScreen(self.dataPersister.user._getNickname(), self.articles, self.activeArticle);
                });
            });
        },
        loadMyArticles: function () {
            var self = this;

            this.dataPersister.article.getAll(10, true).then(function (result) {
                self.myArticles = result;
            }).then(function () {
                self.dataPersister.article.get(self.myArticles[0].id).then(function (result) {
                    self.myActiveArticle = result;
                }).then(function () {
                    self.ui.loadMyMainScreen(self.dataPersister.user._getNickname(), self.myArticles, self.myActiveArticle);
                });
            });
        },
        updateArticles: function () {
            var self = this;

            self.dataPersister.article.getAll(10, false).then(function (result) {
                self.articles = result;
            }).then(function () {
                self.ui.loadArticlesTab(self.articles);
            });
        },
        updateMyArticles: function () {
            var self = this;

            self.dataPersister.article.getAll(10, true).then(function (result) {
                self.myArticles = result;
            }).then(function () {
                self.ui.loadMyArticlesTab(self.articles);
            });
        },
        updateGameField: function (gameId) {
            var self = this;
            this.dataPersister.game.field(gameId).then(function (result) {
                self.ui.updateGameField(result);
            }, function (error) {
                self.showErrorMessage(error);
            });

        },
        showErrorMessage: function (error) {
            alert(error.responseJSON.Message);
        }
    });

    return {
        getControler: function (dataPersister, userInterface) {
            return new Controler(dataPersister, userInterface);
        }
    }
}());